package com.zq.zhufengfm.fragments.discover;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;

import com.zq.zhufengfm.AlbumDetailActivity;
import com.zq.zhufengfm.Constants;
import com.zq.zhufengfm.adapter.DiscoverRecommendAdapter;
import com.zq.zhufengfm.adapter.PicPagerAdapter;
import com.zq.zhufengfm.entity.discoverrecommened.AlbumRecommend;
import com.zq.zhufengfm.entity.discoverrecommened.DiscoverRecommendItem;
import com.zq.zhufengfm.entity.discoverrecommened.DiscoverRecommendTopPicAds;
import com.zq.zhufengfm.fragments.BaseFragment;
import com.zq.zhufengfm.tasks.discover.DiscoverREcommendTask;
import com.zq.zhufengfm.tasks.TaskCallback;
import com.zq.zhufengfm.tasks.TasklResult;
import com.zq.zhufengfm.utils.DimensionUtil;
import com.zq.zhufengfm.utils.EntityParseUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;

/**
 * Project_name : ZhuFengFM
 * Author : zhaoQiang
 * Email : 229457269@qq.com
 * Create_time : 2015/10/20 | 10:57
 */
public class DisCoverRecommendFragment extends BaseFragment implements TaskCallback, View.OnClickListener {

    private DiscoverRecommendAdapter adapter;
    private List<DiscoverRecommendItem> items;

    /**
     *   头图片的   轮播海报
     */
    private ViewPager focusImagesPager;
    //适配数据，，，，
    PicPagerAdapter picPagerAdapter;
    //头图片  的数据信息
    private List picData;

    public DisCoverRecommendFragment() {}

    @Override
    public String getFragmentTitle() {
        return "推荐";
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        items = new LinkedList<DiscoverRecommendItem>();

        adapter = new DiscoverRecommendAdapter(getActivity(),items);

    }

    /**
     * 每次进入   推荐的时候   都会进行推荐的刷新  网络加载放在此处
     */
    @Override
    public void onResume() {
        super.onResume();

        DiscoverREcommendTask task = new DiscoverREcommendTask(this);

        task.execute();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View ret = inflater.inflate(R.layout.fragment_discover_recommend, container, false);

        ListView listView = (ListView)
                ret.findViewById(R.id.discover_recommend_list);

        //创建 ViewPager
        focusImagesPager = new ViewPager(getActivity());

        AbsListView.LayoutParams lp =
                new AbsListView.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        DimensionUtil.dp2px(getActivity(), 160)
                );

        focusImagesPager.setLayoutParams(lp);

        picData = new LinkedList();

        //添加视图：
        listView.addHeaderView(focusImagesPager);

        //-----------------------------

        //设置专辑点击事件：
        adapter.setOnRecommendAlbumClickListener(this);

        //-----------------------------

        listView.setAdapter(adapter);

        return ret;
    }

    //加载完毕
    @Override
    public void onTaskFinished(TasklResult result) {

        if (result != null) {
            //TODO  :处理推荐列表数据
            if (result.action== Constants.TASK_ACTION_DISCOVER_RECOMMENDS) {

                if(result.resultCode == Constants.TASK_RESULT_OK){

                    Object data = result.data;//接收的数据

                    if (data != null) {

//                            if (data instanceof List) {
//                                //TODO 更新listView adapter
//                                //只要数据来了  就清除adapter
//
//                                List list = (List) data;
//
//                                try {
//                                    JSONObject jsonObject = new JSONObject(list.toString());
//
//                                } catch (JSONException e) {
//                                    e.printStackTrace();
//                                }
//
//                                items.clear();
//
//                                for (Object o:list){
//                                    if (o instanceof DiscoverRecommendItem){
//                                        items.add((DiscoverRecommendItem) o);
//                                    }
//                                }
//                                adapter.notifyDataSetChanged();
//                            }else

                            if(data instanceof JSONObject){

                                //focusImages:

                                //1，轮播海报   表头广告
                                JSONObject jsonObject = (JSONObject) data;

                                //加载小编推荐  热门推荐之类的

                                try {

                                    int retCode = jsonObject.getInt("ret");

                                    List<DiscoverRecommendItem> list = EntityParseUtil
                                            .parseDiscoverCommened(jsonObject);
                                    items.clear();
                                    for(Object o : list){
                                        if (o instanceof DiscoverRecommendItem){
                                            items.add((DiscoverRecommendItem) o);
                                        }
                                    }

                                    adapter.notifyDataSetChanged();


                                    //-----------轮番播放广告：
                                    JSONObject object = jsonObject.getJSONObject("focusImages");

                                    DiscoverRecommendTopPicAds topPicAds =
                                            new DiscoverRecommendTopPicAds();

                                    topPicAds.parseJSON(object);

                                    picData = topPicAds.getPicAds();

                                    picPagerAdapter = new PicPagerAdapter(picData);
                                    focusImagesPager.setAdapter(picPagerAdapter);

                                    picPagerAdapter.notifyDataSetChanged();

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                    }
                }else{
                    //TODO 提示错误信息


                }
            }
        }
    }

    @Override
    public void onClick(View v){

        Object tag = v.getTag();

        if (tag != null) {

            if(tag instanceof AlbumRecommend){
                    //也是专辑推荐
                AlbumRecommend albumRecommend = (AlbumRecommend) tag;

                //专辑id
                long albumId = albumRecommend.getAlbumId();//269179

//                //曲目ID
//                long trackId = albumRecommend.getTrackId();

                //打开专辑详情界面：
                /**
                 * 打开一个新的activity
                 */
                Intent intent = new Intent(getActivity(),AlbumDetailActivity.class);
                intent.putExtra(Constants.START_ALBUMDETAIL_ALBUMID_VAR,
                                albumId+"");
                startActivity(intent);

            }
        }

    }
}
